
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Plug · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="eex.html" />
    
    
    <link rel="prev" href="../ecto/querying.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter active" data-level="4.1" data-path="plug.html">
            
                <a href="plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="eex.html">
            
                <a href="eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="ets.html">
            
                <a href="ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="mnesia.html">
            
                <a href="mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="debugging.html">
            
                <a href="debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Plug</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x5982;&#x679C;&#x4F60;&#x719F;&#x6089; Ruby&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x628A; Plug &#x60F3;&#x6210; Rack&#xFF0C;&#x518D;&#x52A0;&#x4E0A;&#x4E00;&#x70B9; Sinatra&#x3002;&#x5B83;&#x63D0;&#x4F9B;&#x4E86;&#x7F16;&#x5199; Web &#x5E94;&#x7528;&#x7EC4;&#x4EF6;&#x7684;&#x4E00;&#x7EC4;&#x89C4;&#x8303;&#xFF0C;&#x4EE5;&#x53CA;&#x63A5;&#x5165; Web &#x670D;&#x52A1;&#x5668;&#x6240;&#x9700;&#x7684;&#x4E00;&#x4E9B;&#x9002;&#x914D;&#x5668;&#x3002;&#x867D;&#x7136; Plug &#x4E0D;&#x5C5E;&#x4E8E; Elixir &#x7684;&#x6838;&#x5FC3;&#x5E93;&#xFF0C;&#x4F46;&#x5B83;&#x4F9D;&#x7136;&#x662F;&#x4E00;&#x4E2A; Elixir &#x5B98;&#x65B9;&#x7EF4;&#x62A4;&#x7684;&#x9879;&#x76EE;&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x672C;&#x8BFE;&#x7A0B;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x4F7F;&#x7528; <code>PlugCowboy</code> &#x6765;&#x4ECE;&#x96F6;&#x5F00;&#x59CB;&#x6253;&#x9020;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684; HTTP &#x670D;&#x52A1;&#x5668;&#x3002;Cowboy &#x662F;&#x4E00;&#x4E2A;&#x4E3A; Erlang &#x6253;&#x9020;&#x7684;&#x7B80;&#x5355;&#x7684; HTTP &#x670D;&#x52A1;&#x5668;&#x3002;&#x800C; Plug &#x5219;&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x5B83;&#x7684; connection &#x9002;&#x914D;&#x3002;</p>
<p>&#x5F53;&#x6781;&#x7B80;&#x7684; web &#x5E94;&#x7528;&#x914D;&#x7F6E;&#x597D;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5B66;&#x4E60; Plug &#x7684; router &#x4EE5;&#x53CA;&#x5982;&#x4F55;&#x5728;&#x5355;&#x4E2A; web &#x5E94;&#x7528;&#x5185;&#x4F7F;&#x7528;&#x591A;&#x4E2A; plugs&#x3002;</p>
<h2 id="&#x73AF;&#x5883;&#x51C6;&#x5907;">&#x73AF;&#x5883;&#x51C6;&#x5907;</h2>
<p>&#x672C;&#x6559;&#x7A0B;&#x5047;&#x8BBE;&#x4F60;&#x5DF2;&#x7ECF;&#x5B89;&#x88C5;&#x4E86; 1.5 &#x7248;&#x672C;&#x4EE5;&#x4E0A;&#x7684; Elixir&#xFF0C;&#x4EE5;&#x53CA; <code>mix</code>&#x3002;</p>
<p>&#x5148;&#x4ECE;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5E26; supervision tree &#x7684; OTP &#x9879;&#x76EE;&#x5F00;&#x59CB;&#xFF1A;</p>
<pre><code class="lang-shell">$ mix new example --sup
$ cd example
</code></pre>
<p>&#x6211;&#x4EEC;&#x7684; Elixir &#x5E94;&#x7528;&#x9700;&#x8981;&#x5305;&#x542B; supervision tree &#x662F;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7528;&#x5230; Supervisor &#x6765;&#x542F;&#x52A8;&#x548C;&#x8FD0;&#x884C;&#x6211;&#x4EEC;&#x7684; Cowboy2 &#x670D;&#x52A1;&#x5668;&#x3002;</p>
<h2 id="&#x4F9D;&#x8D56;">&#x4F9D;&#x8D56;</h2>
<p>&#x4F7F;&#x7528; mix &#x6DFB;&#x52A0;&#x4F9D;&#x8D56;&#x7B80;&#x76F4;&#x6613;&#x5982;&#x53CD;&#x638C;&#x3002;&#x8981;&#x4F7F;&#x7528; Plug &#x4F5C;&#x4E3A; Cowboy2 &#x670D;&#x52A1;&#x5668;&#x7684;&#x63A5;&#x53E3;&#x9002;&#x914D;&#x5668;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5B89;&#x88C5; <code>PlugCowboy</code> &#x5305;&#xFF1A;</p>
<p>&#x6DFB;&#x52A0;&#x4E0B;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x5230;&#x4F60;&#x7684; <code>mix.exs</code> &#x6587;&#x4EF6;&#x91CC;&#x9762;&#x7684;&#x4F9D;&#x8D56;&#x90E8;&#x5206;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">deps</span></span> <span class="hljs-keyword">do</span>
  [
    {<span class="hljs-symbol">:plug_cowboy</span>, <span class="hljs-string">&quot;~&gt; 2.0&quot;</span>},
  ]
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x8FD0;&#x884C;&#x4E0B;&#x9762;&#x7684; mix &#x4EFB;&#x52A1;&#x62C9;&#x53D6;&#x65B0;&#x6DFB;&#x52A0;&#x7684;&#x4F9D;&#x8D56;&#xFF1A;</p>
<pre><code class="lang-shell">$ mix deps.get
</code></pre>
<h2 id="plug-&#x89C4;&#x8303;">Plug &#x89C4;&#x8303;</h2>
<p>&#x5728;&#x7740;&#x624B;&#x521B;&#x5EFA; Plug &#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E86;&#x89E3;&#x5E76;&#x9075;&#x5FAA; Plug &#x6240;&#x6307;&#x5B9A;&#x7684;&#x89C4;&#x8303;&#x3002;&#x867D;&#x7136;&#x542C;&#x8D77;&#x6765;&#x5F88;&#x590D;&#x6742;, &#x4F46;&#x5B9E;&#x9645;&#x4E0A;&#x53EA;&#x6709;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x662F;&#x5FC5;&#x987B;&#x7684;&#xFF1A;<code>init/1</code> &#x548C; <code>call/2</code>&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684; Plug&#xFF0C;&#x5B83;&#x4F1A;&#x8FD4;&#x56DE; &quot;Hello World&quot;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.HelloWorldPlug <span class="hljs-keyword">do</span>
  import Plug.Conn

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(options), <span class="hljs-symbol">do:</span> options

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">call</span></span>(conn, _opts) <span class="hljs-keyword">do</span>
    conn
    |&gt; put_resp_content_type(<span class="hljs-string">&quot;text/plain&quot;</span>)
    |&gt; send_resp(<span class="hljs-number">200</span>, <span class="hljs-string">&quot;Hello World!\n&quot;</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x628A;&#x4E0A;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x4FDD;&#x5B58;&#x5230;&#x6587;&#x4EF6; <code>lib/example/hello_world_plug.ex</code>&#x3002;</p>
<p><code>init/1</code> &#x51FD;&#x6570;&#x4F7F;&#x7528;&#x6765;&#x521D;&#x59CB;&#x5316; Plug &#x7684;&#x914D;&#x7F6E;&#x3002;&#x5B83;&#x4F1A;&#x88AB; supervision tree &#x8C03;&#x7528;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x5B83;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x7A7A;&#x5217;&#x8868;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x6709;&#x4EC0;&#x4E48;&#x5F71;&#x54CD;&#x3002;</p>
<p>&#x4ECE; <code>init/1</code> &#x8FD4;&#x56DE;&#x7684;&#x503C;&#x6700;&#x7EC8;&#x4F1A;&#x4F5C;&#x4E3A;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x4F20;&#x5165;&#x5230; <code>call/2</code> &#x4E2D;&#x3002;</p>
<p>&#x5728;&#x6211;&#x4EEC;&#x7684; web &#x670D;&#x52A1;&#x5668;&#xFF0C;Cowboy&#xFF0C;&#x63A5;&#x6536;&#x5230;&#x6BCF;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8BF7;&#x6C42;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8C03;&#x7528; <code>call/2</code> &#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F; <code>%Plug.Conn{}</code> &#x8FD9;&#x4E2A;&#x8FDE;&#x63A5;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x5B83;&#x4E5F;&#x5E94;&#x8BE5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; <code>%Plug.Conn{}</code> &#x8FDE;&#x63A5;&#x7ED3;&#x6784;&#x4F53;&#x3002;</p>
<h2 id="&#x914D;&#x7F6E;&#x9879;&#x76EE;&#x7684;&#x5E94;&#x7528;&#x6A21;&#x5757;">&#x914D;&#x7F6E;&#x9879;&#x76EE;&#x7684;&#x5E94;&#x7528;&#x6A21;&#x5757;</h2>
<p>&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x5E94;&#x7528;&#x542F;&#x52A8;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x544A;&#x77E5;&#x5B83;&#x542F;&#x52A8;&#x5E76;&#x76D1;&#x63A7; Cowboy web &#x670D;&#x52A1;&#x5668;&#x3002;</p>
<p>&#x8FD9;&#x662F;&#x901A;&#x8FC7; <a href="https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html#child_spec/1" target="_blank"><code>Plug.Cowboy.child_spec/1</code></a> &#x51FD;&#x6570;&#x6765;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x671F;&#x671B;&#x63A5;&#x6536;&#x4E09;&#x4E2A;&#x914D;&#x7F6E;&#x9009;&#x9879;&#xFF1A;</p>
<ul>
<li><code>:scheme</code> - &#x539F;&#x5B50;&#x7C7B;&#x578B;&#x7684; HTTP or HTTPS &#x914D;&#x7F6E;&#xFF08;<code>:http</code>, <code>:https</code>&#xFF09;</li>
<li><code>:plug</code> - &#x5728; web &#x670D;&#x52A1;&#x5668;&#x4E2D;&#x7528;&#x4F5C;&#x4E3A;&#x63A5;&#x53E3;&#x7684; plug &#x6A21;&#x5757;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x6A21;&#x5757;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x6BD4;&#x5982; <code>MyPlug</code>&#xFF0C;&#x6216;&#x8005;&#x662F;&#x6A21;&#x5757;&#x540D;&#x5B57;&#x548C;&#x914D;&#x7F6E;&#x7684;&#x5143;&#x7EC4; <code>{MyPlug, plug_opts}</code>&#xFF0C;<code>plug_opts</code> &#x5C06;&#x4F1A;&#x4F20;&#x5165; plug &#x6A21;&#x5757;&#x7684; <code>init/1</code> &#x51FD;&#x6570;&#x3002;</li>
<li><code>:options</code> - &#x670D;&#x52A1;&#x5668;&#x914D;&#x7F6E;&#x3002;&#x9700;&#x8981;&#x5305;&#x542B;&#x670D;&#x52A1;&#x5668;&#x76D1;&#x542C;&#x548C;&#x63A5;&#x6536;&#x8BF7;&#x6C42;&#x7684;&#x7AEF;&#x53E3;&#x53F7;&#x3002;</li>
</ul>
<p><code>lib/example/application.ex</code> &#x6587;&#x4EF6;&#x9700;&#x8981;&#x5728; <code>start/2</code> &#x51FD;&#x6570;&#x5185;&#x5B9A;&#x4E49;&#x597D;&#x5B50;&#x8FDB;&#x7A0B; Spec&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Application <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Application
  require Logger

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span></span>(_type, _args) <span class="hljs-keyword">do</span>
    children = [
      {Plug.Cowboy, <span class="hljs-symbol">scheme:</span> <span class="hljs-symbol">:http</span>, <span class="hljs-symbol">plug:</span> Example.HelloWorldPlug, <span class="hljs-symbol">options:</span> [<span class="hljs-symbol">port:</span> <span class="hljs-number">8080</span>]}
    ]
    opts = [<span class="hljs-symbol">strategy:</span> <span class="hljs-symbol">:one_for_one</span>, <span class="hljs-symbol">name:</span> Example.Supervisor]

    Logger.info(<span class="hljs-string">&quot;Starting application...&quot;</span>)

    Supervisor.start_link(children, opts)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong>&#x6211;&#x4EEC;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x663E;&#x5F0F;&#x5730;&#x8C03;&#x7528; <code>child_spec</code> &#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4F1A;&#x5728; Supervisor &#x542F;&#x52A8;&#x8FDB;&#x7A0B;&#x65F6;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x5305;&#x62EC;&#x9700;&#x8981;&#x542F;&#x52A8;&#x7684;&#x6A21;&#x5757;&#x540D;&#xFF0C;&#x4EE5;&#x53CA;&#x542F;&#x52A8;&#x9700;&#x8981;&#x7684;&#x914D;&#x7F6E;&#x9009;&#x9879;&#x3002;</p>
<p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x5C06;&#x5728;&#x6211;&#x4EEC;&#x5E94;&#x7528;&#x7684; supervision tree &#x4E0B;&#x542F;&#x52A8;&#x4E86; Cowboy2 &#x670D;&#x52A1;&#x5668;&#x3002;&#x5B83;&#x5728; <code>8080</code> &#x7AEF;&#x53E3;&#x4E0B;&#xFF0C;&#x4EE5; HTTP &#x6A21;&#x5F0F;&#x8FD0;&#x884C;&#x542F;&#x52A8;&#x4E86; Cowboy &#x670D;&#x52A1;&#xFF08;&#x5F53;&#x7136;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x4E3A; HTTPS&#xFF09;&#x3002;<code>Example.HelloWorldPlug</code> &#x88AB;&#x8BBE;&#x5B9A;&#x4E3A;&#x5904;&#x7406;&#x6536;&#x5230;&#x7684;&#x4EFB;&#x4F55;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x7684;&#x63A5;&#x53E3;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x542F;&#x52A8;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#xFF0C;&#x5E76;&#x4E14;&#x53D1;&#x9001;&#x4E00;&#x4E9B;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x7ED9;&#x5B83;&#x5904;&#x7406;&#x4E86;&#xFF01;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x901A;&#x8FC7; <code>--sup</code> &#x6807;&#x7B7E;&#x751F;&#x6210;&#x4E86; OTP &#x5E94;&#x7528;&#xFF0C;<code>application</code> &#x51FD;&#x6570;&#x4F7F;&#x5F97;&#x6211;&#x4EEC;&#x7684; <code>Example</code> &#x5E94;&#x7528;&#x80FD;&#x81EA;&#x52A8;&#x542F;&#x52A8;&#x3002;</p>
<p>&#x5728; <code>mix.exs</code> &#x6587;&#x4EF6;&#x5185;&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">application</span></span> <span class="hljs-keyword">do</span>
  [
    <span class="hljs-symbol">extra_applications:</span> [<span class="hljs-symbol">:logger</span>],
    <span class="hljs-symbol">mod:</span> {Example.Application, []}
  ]
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;&#x4E00;&#x4E2A;&#x6700;&#x7B80;&#x7684;&#xFF0C;&#x57FA;&#x4E8E; Plug &#x7684; web &#x670D;&#x52A1;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x5C31;&#x7EEA;&#x3002;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x8FD0;&#x884C;&#xFF1A;</p>
<pre><code class="lang-shell">$ mix run --no-halt
</code></pre>
<p>&#x7B49;&#x7F16;&#x8BD1;&#x5B8C;&#x6210;&#xFF0C;<code>[info]  Starting application...</code> &#x5728;&#x547D;&#x4EE4;&#x884C;&#x51FA;&#x73B0;&#x540E;&#xFF0C;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x8BBF;&#x95EE; <a href="http://127.0.0.1:8080" target="_blank">http://127.0.0.1:8080</a>&#x3002;&#x6D4F;&#x89C8;&#x5668;&#x9875;&#x9762;&#x5C31;&#x4F1A;&#x663E;&#x793A;&#xFF1A;</p>
<pre><code>Hello World!
</code></pre><h2 id="plugrouter">Plug.Router</h2>
<p>&#x5BF9;&#x4E8E;&#x7EDD;&#x5927;&#x591A;&#x6570;&#x7684;&#x5E94;&#x7528;&#x6765;&#x8BF4;&#xFF0C;&#x6BD4;&#x5982;&#x4E00;&#x4E2A;&#x7F51;&#x7AD9;&#xFF0C;&#x6216;&#x8005; REST API &#x670D;&#x52A1;&#xFF0C;&#x4F60;&#x4F1A;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x5668;&#x628A;&#x4E0D;&#x540C;&#x8DEF;&#x5F84;&#xFF0C;&#x4E0D;&#x540C; HTTP verb &#x7684;&#x8BF7;&#x6C42;&#x8F6C;&#x5230;&#x4E0D;&#x540C;&#x7684;&#x903B;&#x8F91;&#x5904;&#x7406;&#x4E1A;&#x52A1;&#x4E0A;&#x3002;<code>Plug</code> &#x4E5F;&#x5C31;&#x63D0;&#x4F9B;&#x4E86;&#x8FD9;&#x4E48;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x5668;&#x3002;&#x7D27;&#x63A5;&#x7740;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;Elixir &#x5E76;&#x4E0D;&#x9700;&#x8981;&#x7C7B;&#x4F3C; Sinatra &#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x6846;&#x67B6;&#xFF0C;&#x56E0;&#x4E3A; Plug &#x5DF2;&#x7ECF;&#x514D;&#x8D39;&#x63D0;&#x4F9B;&#x7ED9;&#x6211;&#x4EEC;&#x4E86;&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6587;&#x4EF6; <code>lib/example/router.ex</code>&#xFF0C;&#x5E76;&#x628A;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x590D;&#x5236;&#x8FDB;&#x53BB;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Router <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Plug.Router

  plug <span class="hljs-symbol">:match</span>
  plug <span class="hljs-symbol">:dispatch</span>

  get <span class="hljs-string">&quot;/&quot;</span> <span class="hljs-keyword">do</span>
    send_resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;Welcome&quot;</span>)
  <span class="hljs-keyword">end</span>

  match <span class="hljs-number">_</span> <span class="hljs-keyword">do</span>
    send_resp(conn, <span class="hljs-number">404</span>, <span class="hljs-string">&quot;Oops!&quot;</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x662F;&#x4E00;&#x4E2A;&#x6700;&#x57FA;&#x672C;&#x7684;&#x8DEF;&#x7531;&#x5668;&#xFF0C;&#x4F46;&#x662F;&#x6574;&#x6BB5;&#x4EE3;&#x7801;&#x5E94;&#x8BE5;&#x6765;&#x8BF4;&#x8FD8;&#x662F;&#x5BB9;&#x6613;&#x7406;&#x89E3;&#x7684;&#x3002;&#x6211;&#x4EEC;&#x901A;&#x8FC7; <code>use Plug.Router</code> &#x5F15;&#x7528;&#x4E86;&#x4E00;&#x4E2A;&#x5B8F;&#xFF0C;&#x5E76;&#x914D;&#x7F6E;&#x4E86;&#x4E24;&#x4E2A;&#x5185;&#x7F6E;&#x7684; Plugs&#xFF1A;<code>:match</code> &#x548C; <code>:dispatch</code>&#x3002;&#x7136;&#x540E;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x8DEF;&#x7531;&#xFF0C;&#x4E00;&#x4E2A;&#x5904;&#x7406;&#x6839;&#x76EE;&#x5F55;&#x7684; GET &#x8BF7;&#x6C42;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x5339;&#x914D;&#x4E86;&#x5176;&#x5B83;&#x6240;&#x6709;&#x7684;&#x8BF7;&#x6C42;&#x5E76;&#x8FD4;&#x56DE; 404 &#x6D88;&#x606F;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x9700;&#x8981;&#x56DE;&#x5230; <code>lib/example/application.ex</code> &#x6587;&#x4EF6;&#xFF0C;&#x628A; <code>Example.Router</code> &#x6DFB;&#x52A0;&#x5230; web &#x670D;&#x52A1;&#x5668;&#x7684; supervisor tree &#x5F53;&#x4E2D;&#x3002;&#x628A; <code>Example.HelloWorldPlug</code> &#x66FF;&#x6362;&#x4E3A;&#x65B0;&#x7684;&#x8DEF;&#x7531;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span></span>(_type, _args) <span class="hljs-keyword">do</span>
  children = [
    {Plug.Cowboy, <span class="hljs-symbol">scheme:</span> <span class="hljs-symbol">:http</span>, <span class="hljs-symbol">plug:</span> Example.Router, <span class="hljs-symbol">options:</span> [<span class="hljs-symbol">port:</span> <span class="hljs-number">8080</span>]}
  ]
  opts = [<span class="hljs-symbol">strategy:</span> <span class="hljs-symbol">:one_for_one</span>, <span class="hljs-symbol">name:</span> Example.Supervisor]

  Logger.info(<span class="hljs-string">&quot;Starting application...&quot;</span>)

  Supervisor.start_link(children, opts)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x91CD;&#x65B0;&#x542F;&#x52A8;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5982;&#x679C;&#x524D;&#x9762;&#x542F;&#x52A8;&#x7684;&#x8FD8;&#x6CA1;&#x6709;&#x505C;&#x6B62;&#xFF0C;&#x53EF;&#x4EE5;&#x6309; <code>Ctrl+C</code> &#x4E24;&#x6B21;&#x628A;&#x5B83;&#x4E2D;&#x6B62;&#x3002;</p>
<p>&#x73B0;&#x5728;&#xFF0C;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x8F93;&#x5165; <a href="http://127.0.0.1:8080" target="_blank">http://127.0.0.1:8080</a>&#xFF0C;&#x5C31;&#x5E94;&#x8BE5;&#x4F1A;&#x51FA;&#x73B0; <code>Welcome</code> &#x5B57;&#x6837;&#x3002;&#x7136;&#x540E;&#x8BBF;&#x95EE; <a href="http://127.0.0.1:8080/waldo" target="_blank">http://127.0.0.1:8080/waldo</a> &#x6216;&#x8005;&#x5176;&#x5B83;&#x8DEF;&#x5F84;&#xFF0C;&#x8FD4;&#x56DE;&#x5C31;&#x662F; 404 &#x54CD;&#x5E94;&#x5E76;&#x51FA;&#x73B0; <code>Oops!</code> &#x5B57;&#x6837;&#x3002;</p>
<h2 id="&#x521B;&#x5EFA;&#x53E6;&#x4E00;&#x4E2A;-plug">&#x521B;&#x5EFA;&#x53E6;&#x4E00;&#x4E2A; Plug</h2>
<p>&#x5728;&#x4E00;&#x4E2A; web &#x5E94;&#x7528;&#x4E2D;&#x4F7F;&#x7528;&#x591A;&#x4E2A; plug &#x662F;&#x5F88;&#x5E38;&#x89C1;&#x7684;&#x3002;&#x56E0;&#x4E3A;&#x6BCF;&#x4E00;&#x4E2A; plug &#x53EA;&#x4E13;&#x6CE8;&#x4E8E;&#x5B83;&#x81EA;&#x8EAB;&#x63D0;&#x4F9B;&#x7684;&#x529F;&#x80FD;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x80FD;&#x6709;&#x4E00;&#x4E2A; plug &#x5904;&#x7406;&#x8DEF;&#x7531;&#xFF0C;&#x4E00;&#x4E2A; plug &#x9A8C;&#x8BC1;&#x8BF7;&#x6C42;&#x7684;&#x6B63;&#x786E;&#x6027;&#xFF0C;&#x4E00;&#x4E2A; plug &#x8D1F;&#x8D23;&#x6743;&#x9650;&#x8BA4;&#x8BC1;&#x7B49;&#x3002;&#x672C;&#x5C0F;&#x8282;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x518D;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x68C0;&#x67E5;&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x7684; plug&#xFF0C;&#x5E76;&#x914D;&#x7F6E;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x540C;&#x65F6;&#x4F7F;&#x7528; router &#x548C; validation &#x8FD9;&#x4E24;&#x4E2A; plug&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; Plug &#x6765;&#x9A8C;&#x8BC1;&#x8BF7;&#x6C42;&#x4E2D;&#x662F;&#x5426;&#x5305;&#x542B;&#x4E86;&#x6307;&#x5B9A;&#x7684;&#x53C2;&#x6570;&#x3002;&#x901A;&#x8FC7;&#x5728;&#x4E00;&#x4E2A; Plug &#x4E2D;&#x5B9E;&#x73B0;&#x9A8C;&#x8BC1;&#x529F;&#x80FD;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x786E;&#x4FDD;&#x53EA;&#x6709;&#x5408;&#x6CD5;&#x7684;&#x8BF7;&#x6C42;&#x624D;&#x80FD;&#x8FDB;&#x5165;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x3002;&#x6211;&#x4EEC;&#x8981;&#x6C42;&#x8FD9;&#x4E2A; Plug &#x4F7F;&#x7528;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x6765;&#x521D;&#x59CB;&#x5316;&#xFF1A;<code>:paths</code> &#x548C; <code>:fields</code>&#x3002;&#x8FD9;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x5206;&#x522B;&#x8868;&#x793A;&#x54EA;&#x4E9B;&#x8DEF;&#x5F84;&#x9700;&#x8981;&#x88AB;&#x9A8C;&#x8BC1;&#x4EE5;&#x53CA;&#x5408;&#x6CD5;&#x7684;&#x8BF7;&#x6C42;&#x9700;&#x8981;&#x5305;&#x542B;&#x54EA;&#x4E9B;&#x5B57;&#x6BB5;&#x3002;</p>
<p><em>&#x6CE8;&#x610F;</em>&#xFF1A;Plug &#x4F1A;&#x5E94;&#x7528;&#x5230;&#x6240;&#x6709;&#x7684;&#x8BF7;&#x6C42;&#x4E0A;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5BF9;&#x8BF7;&#x6C42;&#x8FDB;&#x884C;&#x8FC7;&#x6EE4;&#xFF0C;&#x53EA;&#x5728;&#x5176;&#x4E2D;&#x4E00;&#x90E8;&#x5206;&#x4E0A;&#x6267;&#x884C;&#x6240;&#x9700;&#x7684;&#x903B;&#x8F91;&#x3002;&#x5BF9;&#x65E0;&#x9700;&#x5904;&#x7406;&#x7684;&#x60C5;&#x51B5;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x4F20;&#x5165;&#x7684;&#x8FDE;&#x63A5;&#x7ED3;&#x6784;&#x5373;&#x53EF;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5148;&#x770B;&#x770B;&#x5B8C;&#x6210;&#x540E;&#x7684; Plug&#xFF0C;&#x7136;&#x540E;&#x8C08;&#x8C08;&#x5B83;&#x662F;&#x5982;&#x4F55;&#x5DE5;&#x4F5C;&#x7684;&#x3002;&#x6211;&#x4EEC;&#x5728; <code>lib/example/plug/verify_request.ex</code> &#x521B;&#x5EFA;&#x8BE5; Plug&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Plug.VerifyRequest <span class="hljs-keyword">do</span>
  <span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">IncompleteRequestError</span></span> <span class="hljs-keyword">do</span>
    <span class="hljs-variable">@moduledoc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
    Error raised when a required field is missing.
    &quot;</span><span class="hljs-string">&quot;&quot;</span>

    defexception <span class="hljs-symbol">message:</span> <span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(options), <span class="hljs-symbol">do:</span> options

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">call</span></span>(%Plug.Conn{<span class="hljs-symbol">request_path:</span> path} = conn, opts) <span class="hljs-keyword">do</span>
    if path <span class="hljs-keyword">in</span> opts[<span class="hljs-symbol">:paths</span>], <span class="hljs-symbol">do:</span> verify_request!(conn.params, opts[<span class="hljs-symbol">:fields</span>])
    conn
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">verify_request!</span></span>(params, fields) <span class="hljs-keyword">do</span>
    verified =
      params
      |&gt; Map.keys()
      |&gt; contains_fields?(fields)

    <span class="hljs-keyword">unless</span> verified, <span class="hljs-symbol">do:</span> raise(IncompleteRequestError)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">contains_fields?</span></span>(keys, fields), <span class="hljs-symbol">do:</span> Enum.all?(fields, &amp;(&amp;<span class="hljs-number">1</span> <span class="hljs-keyword">in</span> keys))
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x9996;&#x5148;&#xFF0C;&#x6CE8;&#x610F;&#x5230;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5F02;&#x5E38; <code>IncompleteRequestError</code>&#x3002;&#x8FD9;&#x6837; Plug &#x4F1A;&#x5728;&#x89E6;&#x53D1;&#x8BE5;&#x5F02;&#x5E38;&#x65F6;&#x4F7F;&#x7528;&#x8BE5;&#x53C2;&#x6570;&#x7684;&#x503C;&#x4F5C;&#x4E3A; HTTP &#x72B6;&#x6001;&#x7801;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x6240;&#x5199;&#x7684; Plug &#x7B2C;&#x4E8C;&#x90E8;&#x5206;&#x5C31;&#x662F; <code>call/2</code> &#x51FD;&#x6570;&#x3002;&#x6211;&#x4EEC;&#x5728;&#x8FD9;&#x91CC;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x6267;&#x884C;&#x9A8C;&#x8BC1;&#x903B;&#x8F91;&#x3002;&#x5373;&#x53EA;&#x6709;&#x5F53;&#x8BE5;&#x8BF7;&#x6C42;&#x7684;&#x8DEF;&#x5F84;&#x5305;&#x542B;&#x5728;&#x6211;&#x4EEC;&#x6307;&#x5B9A;&#x7684; <code>:paths</code> &#x9009;&#x9879;&#x4E2D;&#x65F6;&#x624D;&#x4F1A;&#x6267;&#x884C; <code>verify_request!/2</code>&#x3002;</p>
<p>&#x6700;&#x540E;&#x4E00;&#x90E8;&#x5206;&#x662F;&#x79C1;&#x6709;&#x51FD;&#x6570; <code>verify_request!/2</code>&#xFF0C;&#x5B83;&#x4F1A;&#x9A8C;&#x8BC1;&#x662F;&#x5426;&#x6307;&#x5B9A;&#x7684; <code>:fields</code> &#x90FD;&#x5B58;&#x5728;&#x4E8E;&#x8BF7;&#x6C42;&#x4E2D;&#x3002;&#x5982;&#x679C;&#x6709;&#x7F3A;&#x5931;&#x6211;&#x4EEC;&#x5C31;&#x629B;&#x51FA;&#x4E00;&#x4E2A; <code>IncompleteRequestError</code>&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5C06;&#x628A; Plug &#x914D;&#x7F6E;&#x4E3A;&#x9A8C;&#x8BC1;&#x6240;&#x6709;&#x8BBF;&#x95EE; <code>/upload</code> &#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x770B;&#x5B83;&#x662F;&#x5426;&#x5305;&#x542B; <code>&quot;content&quot;</code> &#x548C; <code>&quot;mimetype&quot;</code>&#x3002;&#x53EA;&#x6709;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x7684;&#x8BF7;&#x6C42;&#x624D;&#x4F1A;&#x6267;&#x884C;&#x76F8;&#x5E94;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x770B;&#x770B;&#x5982;&#x4F55;&#x628A;&#x8FD9;&#x4E2A;&#x8DEF;&#x7531;&#x914D;&#x7F6E;&#x5230; Plug &#x4E0A;&#x9762;&#x53BB;&#x3002;&#x7F16;&#x8F91; <code>lib/example/router.ex</code> &#x5E76;&#x505A;&#x51FA;&#x4EE5;&#x4E0B;&#x6539;&#x53D8;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Router <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Plug.Router

  <span class="hljs-keyword">alias</span> Example.Plug.VerifyRequest

  plug Plug.Parsers, <span class="hljs-symbol">parsers:</span> [<span class="hljs-symbol">:urlencoded</span>, <span class="hljs-symbol">:multipart</span>]
  plug VerifyRequest, <span class="hljs-symbol">fields:</span> [<span class="hljs-string">&quot;content&quot;</span>, <span class="hljs-string">&quot;mimetype&quot;</span>], <span class="hljs-symbol">paths:</span> [<span class="hljs-string">&quot;/upload&quot;</span>]
  plug <span class="hljs-symbol">:match</span>
  plug <span class="hljs-symbol">:dispatch</span>

  get <span class="hljs-string">&quot;/&quot;</span> <span class="hljs-keyword">do</span>
    send_resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;Welcome&quot;</span>)
  <span class="hljs-keyword">end</span>

  get <span class="hljs-string">&quot;/upload&quot;</span> <span class="hljs-keyword">do</span>
    send_resp(conn, <span class="hljs-number">201</span>, <span class="hljs-string">&quot;Uploaded&quot;</span>)
  <span class="hljs-keyword">end</span>

  match <span class="hljs-number">_</span> <span class="hljs-keyword">do</span>
    send_resp(conn, <span class="hljs-number">404</span>, <span class="hljs-string">&quot;Oops!&quot;</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x901A;&#x8FC7;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x5728;&#x8FD0;&#x884C; router &#x7684;&#x4EE3;&#x7801;&#x524D;&#xFF0C;&#x4F1A;&#x628A;&#x63A5;&#x6536;&#x5230;&#x7684;&#x8BF7;&#x6C42;&#x5148;&#x901A;&#x8FC7; <code>VerifyRequest</code> plug&#x3002;&#x8FD9;&#x662F;&#x7531;&#x4EE5;&#x4E0B;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x5B9E;&#x73B0;&#x7684;&#xFF1A;</p>
<pre><code class="lang-elixir">plug(
  VerifyRequest,
  <span class="hljs-symbol">fields:</span> [<span class="hljs-string">&quot;content&quot;</span>, <span class="hljs-string">&quot;mimetype&quot;</span>],
  <span class="hljs-symbol">paths:</span> [<span class="hljs-string">&quot;/upload&quot;</span>]
)
</code></pre>
<p>&#x8FD9;&#x4F1A;&#x53BB;&#x81EA;&#x52A8;&#x8C03;&#x7528; <code>VerifyRequest.init(fields: [&quot;content&quot;, &quot;mimetype&quot;],
paths: [&quot;/upload&quot;])</code>&#x3002;&#x63A5;&#x7740;&#x5C31;&#x4F1A;&#x628A;&#x53C2;&#x6570;&#x4F20;&#x7ED9; <code>VerifyRequest.call(conn, opts)</code> &#x51FD;&#x6570;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x8BD5;&#x9A8C;&#x4E00;&#x4E0B;&#x8FD9;&#x4E2A; Plug&#xFF01;&#x505C;&#x6389;&#x6B63;&#x5728;&#x8FD0;&#x884C;&#x7684;&#x4EE3;&#x7801;&#x540E;&#xFF08;&#x53EF;&#x6309;&#x4E24;&#x6B21; <code>ctrl + c</code>&#xFF09;, &#x518D;&#x91CD;&#x542F;&#x670D;&#x52A1;&#xFF08;<code>mix run --no-halt</code>&#xFF09;&#x3002;&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x8BD5;&#x7740;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x8BBF;&#x95EE; <a href="http://127.0.0.1:8080/upload" target="_blank">http://127.0.0.1:8080/upload</a> &#xFF0C;&#x4F60;&#x4F1A;&#x53D1;&#x73B0;&#x8FD9;&#x4E2A;&#x9875;&#x9762;&#x4E0D;&#x80FD;&#x6B63;&#x5E38;&#x8FD0;&#x4F5C;&#x3002;&#x53EA;&#x6709;&#x6D4F;&#x89C8;&#x5668;&#x63D0;&#x4F9B;&#x7684;&#x9ED8;&#x8BA4;&#x9519;&#x8BEF;&#x9875;&#x9762;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x8BD5;&#x7740;&#x5728;&#x8DEF;&#x5F84;&#x540E;&#x6DFB;&#x52A0;&#x5FC5;&#x987B;&#x7684;&#x53C2;&#x6570; <a href="http://127.0.0.1:8080/upload?content=thing1&amp;mimetype=thing2" target="_blank">http://127.0.0.1:8080/upload?content=thing1&amp;mimetype=thing2</a>&#x3002;&#x52A0;&#x4E0A;&#x5FC5;&#x987B;&#x7684;&#x53C2;&#x6570;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x5E94;&#x8BE5;&#x5C31;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x201D;Uploaded&#x201C;&#x7684;&#x4FE1;&#x606F;&#x4E86;&#x3002;</p>
<p>&#x4F46;&#x662F;&#xFF0C;&#x51FA;&#x9519;&#x4E4B;&#x540E;&#x770B;&#x4E0D;&#x5230;&#x4EFB;&#x4F55;&#x9875;&#x9762;&#x663E;&#x7136;&#x5E76;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x7A0D;&#x540E;&#x4F1A;&#x8BA8;&#x8BBA;&#x5904;&#x7406; Plug &#x9519;&#x8BEF;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<h2 id="http-&#x7AEF;&#x53E3;&#x7684;&#x53EF;&#x914D;&#x7F6E;&#x5316;">HTTP &#x7AEF;&#x53E3;&#x7684;&#x53EF;&#x914D;&#x7F6E;&#x5316;</h2>
<p>&#x6211;&#x4EEC;&#x5728;&#x524D;&#x9762;&#x5B9A;&#x4E49; <code>Example</code> &#x6A21;&#x5757;&#x548C;&#x5E94;&#x7528;&#x7684;&#x65F6;&#x5019;&#xFF0C;HTTP &#x7AEF;&#x53E3;&#x662F;&#x5199;&#x6B7B;&#x5728;&#x6A21;&#x5757;&#x4EE3;&#x7801;&#x91CC;&#x7684;&#x3002;&#x628A; HTTP &#x7AEF;&#x53E3;&#x653E;&#x5230;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x505A;&#x6210;&#x53EF;&#x914D;&#x7F6E;&#x5316;&#x662F;&#x6BD4;&#x8F83;&#x597D;&#x7684;&#x505A;&#x6CD5;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x628A;&#x5E94;&#x7528;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x8BBE;&#x7F6E;&#x5230; <code>config/config.exs</code></p>
<pre><code class="lang-elixir"><span class="hljs-keyword">use</span> Mix.Config

config <span class="hljs-symbol">:example</span>, <span class="hljs-symbol">cowboy_port:</span> <span class="hljs-number">8080</span>
</code></pre>
<p>&#x4E0B;&#x4E00;&#x6B65;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x66F4;&#x65B0; <code>lib/example/application.ex</code> &#x6765;&#x8BFB;&#x53D6;&#x7AEF;&#x53E3;&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x5E76;&#x4F20;&#x7ED9; Cowboy&#x3002;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x79C1;&#x6709;&#x7684;&#x51FD;&#x6570;&#x6765;&#x4E13;&#x95E8;&#x8D1F;&#x8D23;&#x8FD9;&#x90E8;&#x5206;&#x529F;&#x80FD;&#x3002;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Application <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Application
  require Logger

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span></span>(_type, _args) <span class="hljs-keyword">do</span>
    children = [
      {Plug.Cowboy, <span class="hljs-symbol">scheme:</span> <span class="hljs-symbol">:http</span>, <span class="hljs-symbol">plug:</span> Example.Router, <span class="hljs-symbol">options:</span> [<span class="hljs-symbol">port:</span> cowboy_port()]}
    ]
    opts = [<span class="hljs-symbol">strategy:</span> <span class="hljs-symbol">:one_for_one</span>, <span class="hljs-symbol">name:</span> Example.Supervisor]

    Logger.info(<span class="hljs-string">&quot;Starting application...&quot;</span>)

    Supervisor.start_link(children, opts)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">cowboy_port</span></span>, <span class="hljs-symbol">do:</span> Application.get_env(<span class="hljs-symbol">:example</span>, <span class="hljs-symbol">:cowboy_port</span>, <span class="hljs-number">8080</span>)
<span class="hljs-keyword">end</span>
</code></pre>
<p><code>Application.get_env</code> &#x7684;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x5F53;&#x914D;&#x7F6E;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x65F6;&#x5019;&#x91C7;&#x7528;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E0B;&#x9762;&#x7684;&#x547D;&#x4EE4;&#x6765;&#x542F;&#x52A8;&#x5E94;&#x7528;&#xFF1A;</p>
<pre><code class="lang-shell">$ mix run --no-halt
</code></pre>
<h2 id="&#x6D4B;&#x8BD5;-plug">&#x6D4B;&#x8BD5; Plug</h2>
<p>&#x501F;&#x52A9; <code>Plug.Test</code> &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5F88;&#x76F4;&#x89C2;&#x5730;&#x6D4B;&#x8BD5; Plug&#x3002;&#x8BE5;&#x6A21;&#x5757;&#x5305;&#x62EC;&#x4E86;&#x8BB8;&#x591A;&#x65B9;&#x4FBF;&#x6D4B;&#x8BD5;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x628A;&#x4E0B;&#x9762;&#x8FD9;&#x6BB5;&#x8DEF;&#x7531;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x5199;&#x5230; <code>test/example/router_test.exs</code>&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.RouterTest <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case
  <span class="hljs-keyword">use</span> Plug.Test

  <span class="hljs-keyword">alias</span> Example.Router

  <span class="hljs-variable">@content</span> <span class="hljs-string">&quot;&lt;html&gt;&lt;body&gt;Hi!&lt;/body&gt;&lt;/html&gt;&quot;</span>
  <span class="hljs-variable">@mimetype</span> <span class="hljs-string">&quot;text/html&quot;</span>

  <span class="hljs-variable">@opts</span> Router.init([])

  test <span class="hljs-string">&quot;returns welcome&quot;</span> <span class="hljs-keyword">do</span>
    conn =
      <span class="hljs-symbol">:get</span>
      |&gt; conn(<span class="hljs-string">&quot;/&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)
      |&gt; Router.call(<span class="hljs-variable">@opts</span>)

    assert conn.state == <span class="hljs-symbol">:sent</span>
    assert conn.status == <span class="hljs-number">200</span>
  <span class="hljs-keyword">end</span>

  test <span class="hljs-string">&quot;returns uploaded&quot;</span> <span class="hljs-keyword">do</span>
    conn =
      <span class="hljs-symbol">:get</span>
      |&gt; conn(<span class="hljs-string">&quot;/upload?content=<span class="hljs-subst">#{<span class="hljs-variable">@content</span>}</span>&amp;mimetype=<span class="hljs-subst">#{<span class="hljs-variable">@mimetype</span>}</span>&quot;</span>)
      |&gt; Router.call(<span class="hljs-variable">@opts</span>)

    assert conn.state == <span class="hljs-symbol">:sent</span>
    assert conn.status == <span class="hljs-number">201</span>
  <span class="hljs-keyword">end</span>

  test <span class="hljs-string">&quot;returns 404&quot;</span> <span class="hljs-keyword">do</span>
    conn =
      <span class="hljs-symbol">:get</span>
      |&gt; conn(<span class="hljs-string">&quot;/missing&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)
      |&gt; Router.call(<span class="hljs-variable">@opts</span>)

    assert conn.state == <span class="hljs-symbol">:sent</span>
    assert conn.status == <span class="hljs-number">404</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x4EE5;&#x4E0B;&#x7684;&#x547D;&#x4EE4;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-shell">$ mix test test/example/router_test.exs
</code></pre>
<h2 id="plugerrorhandler">Plug.ErrorHandler</h2>
<p>&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x4E0D;&#x5E26;&#x53C2;&#x6570;&#x8BBF;&#x95EE; <a href="http://127.0.0.1:8080/upload" target="_blank">http://127.0.0.1:8080/upload</a> &#x65F6;&#xFF0C;&#x65E0;&#x6CD5;&#x770B;&#x5230;&#x53CB;&#x597D;&#x9519;&#x8BEF;&#x9875;&#x9762;&#x6216;&#x6709;&#x6548;&#x7684; HTTP &#x72B6;&#x6001; &#x2014;&#x2014; &#x53EA;&#x6709;&#x6D4F;&#x89C8;&#x5668;&#x7684; <code>500 Internal Server Error</code> &#x9ED8;&#x8BA4;&#x9519;&#x8BEF;&#x9875;&#x9762;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x501F;&#x52A9; <a href="https://hexdocs.pm/plug/Plug.ErrorHandler.html" target="_blank"><code>Plug.ErrorHandler</code></a> &#x6765;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3002;
&#x9996;&#x5148;&#xFF0C;&#x6253;&#x5F00; <code>lib/example/router.ex</code> &#xFF0C;&#x4FEE;&#x6539;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Router <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Plug.Router
  <span class="hljs-keyword">use</span> Plug.ErrorHandler

  <span class="hljs-keyword">alias</span> Example.Plug.VerifyRequest

  plug Plug.Parsers, <span class="hljs-symbol">parsers:</span> [<span class="hljs-symbol">:urlencoded</span>, <span class="hljs-symbol">:multipart</span>]
  plug VerifyRequest, <span class="hljs-symbol">fields:</span> [<span class="hljs-string">&quot;content&quot;</span>, <span class="hljs-string">&quot;mimetype&quot;</span>], <span class="hljs-symbol">paths:</span> [<span class="hljs-string">&quot;/upload&quot;</span>]
  plug <span class="hljs-symbol">:match</span>
  plug <span class="hljs-symbol">:dispatch</span>

  get <span class="hljs-string">&quot;/&quot;</span> <span class="hljs-keyword">do</span>
    send_resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;Welcome&quot;</span>)
  <span class="hljs-keyword">end</span>

  get <span class="hljs-string">&quot;/upload&quot;</span> <span class="hljs-keyword">do</span>
    send_resp(conn, <span class="hljs-number">201</span>, <span class="hljs-string">&quot;Uploaded&quot;</span>)
  <span class="hljs-keyword">end</span>

  match <span class="hljs-number">_</span> <span class="hljs-keyword">do</span>
    send_resp(conn, <span class="hljs-number">404</span>, <span class="hljs-string">&quot;Oops!&quot;</span>)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">handle_errors</span></span>(conn, %{<span class="hljs-symbol">kind:</span> kind, <span class="hljs-symbol">reason:</span> reason, <span class="hljs-symbol">stack:</span> stack}) <span class="hljs-keyword">do</span>
    IO.inspect(kind, <span class="hljs-symbol">label:</span> <span class="hljs-symbol">:kind</span>)
    IO.inspect(reason, <span class="hljs-symbol">label:</span> <span class="hljs-symbol">:reason</span>)
    IO.inspect(stack, <span class="hljs-symbol">label:</span> <span class="hljs-symbol">:stack</span>)
    send_resp(conn, conn.status, <span class="hljs-string">&quot;Something went wrong&quot;</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5728;&#x6A21;&#x5757;&#x7684;&#x4E00;&#x5F00;&#x59CB;&#xFF0C;&#x6211;&#x4EEC;&#x52A0;&#x4E0A;&#x4E86; <code>use Plug.ErrorHandler</code>. &#x8FD9;&#x4E2A; Plug &#x4F1A;&#x6355;&#x6349;&#x6240;&#x6709;&#x7684;&#x9519;&#x8BEF;&#x7136;&#x540E;&#x8C03;&#x7528;&#x51FD;&#x6570; <code>handle_errors/2</code> &#x6765;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x9519;&#x8BEF;&#x3002;<code>handle_errors</code> &#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4E3A; <code>conn</code>&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x62EC; <code>:kind</code>&#xFF0C;<code>:reason</code> &#x548C; <code>:stack</code> &#x952E;&#x503C;&#x7684;&#x6620;&#x5C04;&#x3002;
&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x7B80;&#x5355;&#x7684; <code>handle_errors</code> &#x6765;&#x68C0;&#x67E5;&#x51FA;&#x9519;&#x65F6;&#x7684;&#x5404;&#x79CD;&#x4FE1;&#x606F;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x91CD;&#x542F;&#x670D;&#x52A1;&#x6765;&#x8BD5;&#x9A8C;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#xFF01;</p>
<p>&#x73B0;&#x5728;&#xFF0C;&#x4F60;&#x518D;&#x8BBF;&#x95EE; <a href="http://127.0.0.1:8080/upload" target="_blank">http://127.0.0.1:8080/upload</a>&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x770B;&#x5230;&#x201C;Something went wrong&#x201D;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x3002;&#x56DE;&#x5230;&#x4EE3;&#x7801;&#x7EC8;&#x7AEF;&#xFF0C;&#x4F60;&#x4F1A;&#x770B;&#x5230;&#x5982;&#x4E0B;&#x7684;&#x4FE1;&#x606F;&#xFF1A;</p>
<pre><code class="lang-shell">kind: :error
reason: %Example.Plug.VerifyRequest.IncompleteRequestError{message: &quot;&quot;}
stack: [
  {Example.Plug.VerifyRequest, :verify_request!, 2,
   [file: &apos;lib/example/plug/verify_request.ex&apos;, line: 23]},
  {Example.Plug.VerifyRequest, :call, 2,
   [file: &apos;lib/example/plug/verify_request.ex&apos;, line: 13]},
  {Example.Router, :plug_builder_call, 2,
   [file: &apos;lib/example/router.ex&apos;, line: 1]},
  {Example.Router, :call, 2, [file: &apos;lib/plug/error_handler.ex&apos;, line: 64]},
  {Plug.Cowboy.Handler, :init, 2,
   [file: &apos;lib/plug/cowboy/handler.ex&apos;, line: 12]},
  {:cowboy_handler, :execute, 2,
   [
     file: &apos;/path/to/project/example/deps/cowboy/src/cowboy_handler.erl&apos;,
     line: 41
   ]},
  {:cowboy_stream_h, :execute, 3,
   [
     file: &apos;/path/to/project/example/deps/cowboy/src/cowboy_stream_h.erl&apos;,
     line: 293
   ]},
  {:cowboy_stream_h, :request_process, 3,
   [
     file: &apos;/path/to/project/example/deps/cowboy/src/cowboy_stream_h.erl&apos;,
     line: 271
   ]}
]
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x662F;&#x56DE;&#x9001;&#x4E86; <code>500 Internal Server Error</code> &#x8FD9;&#x6837;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x901A;&#x8FC7;&#x6DFB;&#x52A0; <code>:plug_status</code> &#x5B57;&#x6BB5;&#x5230;&#x6211;&#x4EEC;&#x7684;&#x5F02;&#x5E38;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x5B9A;&#x5236;&#x72B6;&#x6001;&#x7801;&#x3002;&#x6253;&#x5F00; <code>lib/example/plug/verify_request.ex</code>&#xFF0C;&#x66F4;&#x6539;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">IncompleteRequestError</span></span> <span class="hljs-keyword">do</span>
  defexception <span class="hljs-symbol">message:</span> <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-symbol">plug_status:</span> <span class="hljs-number">400</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x91CD;&#x542F;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5237;&#x65B0;&#x9875;&#x9762;&#xFF0C;&#x5C31;&#x4F1A;&#x51FA;&#x73B0; <code>400 Bad Request</code> &#x8FD9;&#x6837;&#x7684;&#x6D88;&#x606F;&#x4E86;&#x3002;</p>
<p>&#x8FD9;&#x4E2A; Plug &#x53EF;&#x4EE5;&#x8BA9;&#x5F00;&#x53D1;&#x8005;&#x975E;&#x5E38;&#x5BB9;&#x6613;&#x5730;&#x67E5;&#x770B;&#x6709;&#x7528;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#xFF0C;&#x4ECE;&#x800C;&#x89E3;&#x51B3;&#x95EE;&#x9898;&#x3002;&#x540C;&#x65F6;&#x4E5F;&#x7ED9;&#x7EC8;&#x7AEF;&#x7528;&#x6237;&#x4E00;&#x4E2A;&#x826F;&#x597D;&#x7684;&#x9875;&#x9762;&#x4F53;&#x9A8C;&#xFF0C;&#x800C;&#x4E0D;&#x81F3;&#x4E8E;&#x89C9;&#x5F97;&#x6574;&#x4E2A;&#x7F51;&#x7AD9;&#x57AE;&#x6389;&#xFF01;</p>
<h2 id="&#x53EF;&#x7528;&#x7684;-plug">&#x53EF;&#x7528;&#x7684; Plug</h2>
<p>&#x6709;&#x8BB8;&#x591A; Plugs &#x90FD;&#x662F;&#x9ED8;&#x8BA4;&#x63D0;&#x4F9B;&#x7684;&#x3002;Plug <a href="https://github.com/elixir-lang/plug#available-plugs" target="_blank">&#x6587;&#x6863;</a>&#x91CC;&#x6709;&#x5B8C;&#x6574;&#x7684;&#x5217;&#x8868;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../ecto/querying.html" class="navigation navigation-prev " aria-label="Previous page: 查询">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="eex.html" class="navigation navigation-next " aria-label="Next page: 嵌入的 Elixir(EEx)">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"2.2.0","title":"Plug","level":"4.1","depth":1,"next":{"title":"嵌入的 Elixir(EEx)","level":"4.2","depth":1,"path":"lessons/specifics/eex.md","ref":"lessons/specifics/eex.md","articles":[]},"previous":{"title":"查询","level":"3.4","depth":1,"path":"lessons/ecto/querying.md","ref":"lessons/ecto/querying.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/specifics/plug.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

